﻿using UnityEngine;
using System.Collections;

public class Marquee : MonoBehaviour
{
    public UILabel textContent;
    public GameObject aniContent;
    public TweenAlpha tAlpha;

    public int moveX=-2;
    public int willMoveX = 0;
    public int currMoveX = 0;
    public int currNeedMove = 2;
    public bool inited = false;
    public void Init(string str)
    {
        moveX = -Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MarqueeSpeed;
        currNeedMove = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MarqueeCount;
        textContent.text = str;

        if(string.IsNullOrEmpty(str))
        {
            Destroy(this.gameObject);
            return;
        }

        willMoveX = 545 + textContent.width;
        aniContent.transform.localPosition = Vector3.zero;
        currMoveX = 0;
        tAlpha.ResetToBeginning();
        tAlpha.PlayForward();
        inited = true;
    }
    
    void Update()
    {
        if (inited)
            return;

        aniContent.transform.localPosition += new Vector3(moveX, 0, 0);
        currMoveX +=  Mathf.Abs( moveX);
        if(currMoveX >= willMoveX)
        {
            currMoveX = 0;
            aniContent.transform.localPosition = Vector3.zero;
            currNeedMove--;
            if(currNeedMove <=0)
            {
                tAlpha.ResetToBeginning();
                tAlpha.PlayReverse();
                Invoke("DestroyMe", tAlpha.duration);
                
            }
        }
    }

    void DestroyMe()
    {
        Destroy(this.gameObject);
    }
}
